package com.myit.mainCode;

import com.myit.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        ArrayList<Student> list = new ArrayList<Student>();

        //lo给循环起名字
        lo:
        while (true) {
            System.out.println("菜单");
            System.out.println("1添加学生");
            System.out.println("2修改学生");
            System.out.println("3查看学生");
            System.out.println("4删除学生");
            System.out.println("5确定退出");
            System.out.println("----请输入序号");

            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    updateStudent(list);
                    break;
                case 3:
                    queryStudent(list);
                    break;
                case 4:
                    deleteStudent(list);
                    break;
                case 5:
                    System.out.println("感谢你的使用~~~");
                    break lo;
                default:
                    System.out.println("别乱输入！！！！");
                    break;
            }
        }

    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        int sid;//定义非局部变量
        while (true) {
            System.out.println("请输入学号");
            sid = sc.nextInt();

            //添加学号必须注意是否重复
            int index = foundIndex(list, sid);//函数作用是在list里面搜索sid是否存在,不存在返回-1，存在返回index
            if (index == -1) {
                break;
            } else {
                System.out.println("学号已经重复");
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();

        //将键盘录入Student的信息装进ArrayList中
        Student stu = new Student(name, sid, age, birthday);
        list.add(stu);
        System.out.println("添加上了");
    }

    public static int foundIndex(ArrayList<Student> list, int sid) {
        for (int i = 0; i < list.size(); i++) {//遍历学生list
            Student stu = list.get(i);//从list拿出来index是i的Student
            if (stu.getSid() == sid) {//对比sid是否一样
                return i;
            }
        }
        return -1;
    }


    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入需要修改的学号");
        int sid = sc.nextInt();

        //添加学号必须注意是否重复
        int index = foundIndex(list, sid);//函数作用是在list里面搜索sid是否存在,不存在返回-1，存在返回index
        if (index == -1) {
            System.out.println("无此学号");
        } else {
            System.out.println("请输入姓名");
            String name = sc.next();
            System.out.println("请输入年龄");
            int age = sc.nextInt();
            System.out.println("请输入生日");
            String birthday = sc.next();

            //将键盘录入Student的信息装进ArrayList中
            Student stu = new Student(name, sid, age, birthday);
            list.set(index, stu);//set方法将index处的元素更换为第二个参数
            System.out.println("修改完成！！！！");
        }


    }

    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("系统中没有学生信息");
            return;
        }
        System.out.println("姓名 学号 年龄 生日");
        System.out.println("姓名\t\t学号\t\t年龄\t\t生日");

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName() + "\t" + stu.getSid() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }

    }

    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除学生的学号");
        int did = sc.nextInt();
        int index = foundIndex(list, did);
        if (index == -1) {
            System.out.println("无此学号！！！！");
        } else {
            list.remove(index);
            System.out.println("删除成功");
        }

    }
}
